import { DB } from "@/lib/db";
import { faker } from "@faker-js/faker";
import { userProfiles, UserProfileSchema } from "@supersavely/db/schema";
import { db } from "../seed";

const userProfileMock = async () => {
  const data: Omit<
    Extract<UserProfileSchema, { mode: "addUserProfile" }>,
    "mode"
  >[] = [];

  const [usersData, gendersData, mobileCountryData] = await Promise.all([
    db.query.users.findMany(),
    db.query.genders.findMany(),
    db.query.mobileCountry.findMany(),
  ]);

  usersData.map((user) => {
    data.push({
      userId: user.id,
      firstName: faker.person.firstName(),
      midName: faker.person.middleName(),
      lastName: faker.person.lastName(),
      genderId: faker.helpers.arrayElement(gendersData).id,
      address: faker.location.streetAddress(),
      mobileCountryCodeId: faker.helpers.arrayElement(mobileCountryData).id,
      mobile: faker.phone.number(),
      birthday: faker.date.birthdate(),
    });
  });

  return data;
};

export async function seed(db: DB) {
  console.log("start seeding userProfiles");
  const userProfilesData = await userProfileMock();
  await db.insert(userProfiles).values(userProfilesData);
}
